package com.mashibing.mall.product.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.mashibing.mall.product.entity.AttrEntity;
import com.mashibing.mall.product.service.CategoryService;
import com.mashibing.mall.product.vo.AttrGroupResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mashibing.common.utils.PageUtils;
import com.mashibing.common.utils.Query;

import com.mashibing.mall.product.dao.AttrGroupDao;
import com.mashibing.mall.product.entity.AttrGroupEntity;
import com.mashibing.mall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrServiceImpl attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<>()
        );
        return new PageUtils(page);
    }

    /**
     * 属性分组 - 分页
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        // 获取检索关键字
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> attrGroupQw = new QueryWrapper<>();
        // 如果检索关键字不为空
        if (ObjectUtil.isNotEmpty(key)) {
            // 拼接查询条件
            attrGroupQw.and(obj -> {
                obj.eq("attr_group_id", key)
                    .or()
                    .like("attr_group_name", key);
            });
        }

        // 如果分类 id 不为 0，则按照分类 id 查询
        if (ObjectUtil.notEqual(catelogId, 0L)) {
            attrGroupQw.eq("catelog_id", catelogId);
        }

        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),attrGroupQw
        );

        return new PageUtils(page);
    }

    /**
     * 属性分组 - 详情
     */
    @Override
    public AttrGroupEntity detail(Long attrGroupId) {
        AttrGroupEntity attrGroup = this.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] paths = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(paths);
        return attrGroup;
    }

    /**
     * 属性分组 - 根据三级分类 id 查询属性信息
     */
    @Override
    public List<AttrGroupResult> getAttrgroupWithAttrsByCatelogId(Long catelogId) {

        return this.list(
                new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId)
        ).stream().map(attrGroup -> {
            AttrGroupResult vo = new AttrGroupResult();
            BeanUtils.copyProperties(attrGroup, vo);
            // 根据属性分组查找所有属性信息
            List<AttrEntity> attrEntities = attrService.getRelationAttr(attrGroup.getAttrGroupId());
            vo.setAttrs(attrEntities);
            return vo;
        }).collect(Collectors.toList());
    }
}
